package guid

import (
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	apis2 "gluenet/pkg/apis"
	"net/http"
)

func register(c apis2.GlueContext) {
	c.Logger.Debugf("Receive GUID Register")

	// 返回
	d := md5.Sum(c.RpcContext.Data.Data)
	guid := hex.EncodeToString(d[:])
	if err := responseOk(&c, guid); err != nil {
		c.Logger.Errorf("response guid %v", err)
	}
}

func unRegister(c apis2.GlueContext) {
	c.Logger.Debugf("Receive GUID UnRegister")
	// 返回
}

func responseOk(c *apis2.GlueContext, data interface{}) error {
	resp := apis2.IResponse{
		Code:    http.StatusOK,
		Message: "succeed",
		Data:    data,
	}
	bts, err := json.Marshal(resp)
	if err != nil {
		return err
	}
	return c.RpcContext.Data.IRespond(bts)
}

func responseErr(c *apis2.GlueContext, err error) error {
	resp := apis2.IResponse{
		Code:    http.StatusBadRequest,
		Message: err.Error(),
		Data:    nil,
	}
	bts, err := json.Marshal(resp)
	if err != nil {
		return err
	}
	return c.RpcContext.Data.IRespond(bts)
}
